1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * DateRangeParamTest.java
6    *
7    * Created on 7-Dec-2004 at 12:41:22 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.util.HashSet;
12  import java.util.Set;
13  
14  import junit.framework.TestCase;
15  import ca.uhn.cache.IDimension;
16  
17  
18  /***
19   * TODO complete javadoc for 
20   * 
21   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
22   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:47 $ by $Author: bryan_tripp $
23   */
24  public class StringSetParamTest extends TestCase {
25      
26      //enable assertions
27      static {
28          ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
29      }
30      
31      private IDimension myDimension1;
32      private IDimension myDimension2;
33      
34      /***
35       * @see junit.framework.TestCase#setUp()
36       */
37      protected void setUp() throws Exception {
38          super.setUp();
39          myDimension1 = new Dimension( "dimension1", new Class[] { StringParam.class, StringSetParam.class } );
40          myDimension2 = new Dimension( "dimension2", new Class[] { StringParam.class, StringSetParam.class } );
41      }
42      
43      /***
44       * @see junit.framework.TestCase#tearDown()
45       */
46      protected void tearDown() throws Exception {
47          super.tearDown();
48      }
49      
50      /***
51       */
52      public void test1_1() {
53          try {
54              StringSetParam ssParam = new StringSetParam( null, new HashSet() );
55              fail( "AssertionError must be thrown" );
56          }
57          catch (AssertionError e) {
58          }
59      }
60      
61      /***
62       */
63      public void test1_2() {
64          try {
65              StringSetParam ssParam = new StringSetParam( myDimension1, (Set) null );
66              fail( "AssertionError must be thrown" );
67          }
68          catch (AssertionError e) {
69          }
70      }
71      
72      /***
73       */
74      public void test1_3() {
75          try {
76              StringSetParam ssParam = new StringSetParam( myDimension1, (String[]) null );
77              fail( "AssertionError must be thrown" );
78          }
79          catch (AssertionError e) {
80          }
81      }
82      
83      /***
84       */
85      public void test3() {
86          Set sspValues = new HashSet();
87          sspValues.add( "value1" );
88          sspValues.add( "value2" );
89          
90          StringSetParam ssParam = new StringSetParam( myDimension1, sspValues );
91          
92          assertEquals( myDimension1, ssParam.getDimension() );
93          assertEquals( sspValues, ssParam.getValues() );
94      }
95      
96      /***
97       */
98      public void test4() {
99          StringSetParam ssParam1 = new StringSetParam( myDimension1, new HashSet() );
100         StringSetParam ssParam2 = new StringSetParam( myDimension1, new HashSet() );
101         
102         assertTrue( ssParam1.intersects(ssParam2) );
103         assertTrue( ssParam2.intersects(ssParam1) );
104     }
105     
106     /***
107      */
108     public void test5() {
109         Set ssParamValues1 = new HashSet();
110         ssParamValues1.add( "a" );
111         ssParamValues1.add( "b" );
112        
113         StringSetParam ssParam1 = 
114             new StringSetParam( myDimension1, ssParamValues1 );
115         
116         Set ssParamValues2 = new HashSet();
117         ssParamValues2.add( "c" );
118         ssParamValues2.add( "d" );
119 
120         StringSetParam ssParam2 = 
121             new StringSetParam( myDimension1, ssParamValues2 );
122         
123         assertFalse( ssParam1.intersects(ssParam2) );
124         assertFalse( ssParam2.intersects(ssParam1) );
125     }
126     
127     /***
128      */
129     public void test6() {
130         Set ssParamValues1 = new HashSet();
131         ssParamValues1.add( "a" );
132         ssParamValues1.add( "b" );
133        
134         StringSetParam ssParam1 = 
135             new StringSetParam( myDimension1, ssParamValues1 );
136         
137         Set ssParamValues2 = new HashSet();
138         ssParamValues2.add( "b" );
139         ssParamValues2.add( "c" );
140 
141         StringSetParam ssParam2 = 
142             new StringSetParam( myDimension1, ssParamValues2 );
143         
144         assertTrue( ssParam1.intersects(ssParam2) );
145         assertTrue( ssParam2.intersects(ssParam1) );
146     }
147 
148     /***
149      */
150     public void test7() {
151         StringSetParam ssParam1 = new StringSetParam( myDimension1, new HashSet() );
152         StringSetParam ssParam2 = new StringSetParam( myDimension2, new HashSet() );
153         
154         assertFalse( ssParam1.intersects(ssParam2) );
155         assertFalse( ssParam2.intersects(ssParam1) );
156     }
157     
158     /***
159      */
160     public void testPointIntersect() {
161         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
162         StringParam point = new StringParam(myDimension1, "c");
163         assertFalse(set.intersects(point));
164         
165         set = new StringSetParam(myDimension1, new String[] {"a", "b"});
166         point = new StringParam(myDimension1, "b");
167         assertTrue(set.intersects(point));
168     }
169     
170     /***
171      */
172     public void testSetDistance() {
173         StringSetParam set1 = new StringSetParam(myDimension1, new String[] {"a", "b"});
174         StringSetParam set2 = new StringSetParam(myDimension1, new String[] {"c", "d"});
175         assertTrue(set1.getDistance(set2, null) - 0.5 < 0.0001);
176 
177         set1 = new StringSetParam(myDimension1, new String[] {"a", "b"});
178         set2 = new StringSetParam(myDimension1, new String[] {"b", "c"});
179         assertTrue(set1.getDistance(set2, null) < 0.0001);
180     }
181     
182     /***
183      */
184     public void testPointDistance() {
185         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
186         StringParam point = new StringParam(myDimension1, "c");
187         assertTrue(set.getDistance(point, null) - 0.5 < 0.0001);
188 
189         set = new StringSetParam(myDimension1, new String[] {"a", "b"});
190         point = new StringParam(myDimension1, "b");
191         assertTrue(set.getDistance(point, null) < 0.0001);        
192     }
193 
194     /***
195      */
196     public void testSetMerge() {
197         StringSetParam set1 = new StringSetParam(myDimension1, new String[] {"a", "b"});
198         StringSetParam set2 = new StringSetParam(myDimension1, new String[] {"c", "d"});
199         StringSetParam result = (StringSetParam) set1.merge(set2);
200         assertTrue(result.getValues().contains("a"));
201         assertTrue(result.getValues().contains("d"));
202     }
203     
204     /***
205      */
206     public void testPointMerge() {
207         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
208         StringParam point = new StringParam(myDimension1, "c");
209         StringSetParam result = (StringSetParam) set.merge(point);
210         assertTrue(result.getValues().contains("a"));
211         assertTrue(result.getValues().contains("c"));
212     }
213 
214 }
215